﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CPP.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    public class CPPPlantAppService : AsyncCrudAppService<CPPPlant, CPPPlantDto, int, PagedResultRequestDto, CreateCPPPlantDto, UpdateCPPPlantDto>, ICPPPlantAppService
    {
        private readonly IObjectMapper _objectMapper;
        public CPPPlantAppService(
            IRepository<CPPPlant, int> repository,
            IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        /// <summary>
        /// 查询站点信息
        /// </summary>
        /// <param name="skipCount">跳过条数</param>
        /// <param name="maxResultCount">单次最大查询数量</param>
        /// <param name="num">承运商名称</param>
        /// <returns></returns>
        public async Task<PagedResultDto<CPPPlantDto>> GetList(int skipCount = 0, int maxResultCount = 10, string num = "")
        {
            PagedResultDto<CPPPlantDto> resultDto = new PagedResultDto<CPPPlantDto>();
            List<CPPPlant> lists = new List<CPPPlant>();
            Expression<Func<CPPPlant, bool>> expr = null;
            if (!string.IsNullOrEmpty(num))
            {
                expr = expr == null ? o => o.Name == num : expr.ExpressionAnd(o => o.Name == num);
            }
            if (expr != null)
            {
                lists = await Repository.GetAllListAsync(expr);
                var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
                resultDto.Items = _objectMapper.Map<List<CPPPlantDto>>(dtos);
                resultDto.TotalCount = lists.Count;
            }
            else
            {
                resultDto = await base.GetAllAsync(new PagedResultRequestDto { MaxResultCount = maxResultCount, SkipCount = skipCount });
            }
            return resultDto;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">id</param>
        /// <returns></returns>
        public async Task BatchDelete(int[] ids)
        {
            if (ids != null && ids.Count() > 0)
            {
                await Repository.DeleteAsync(it => ids.Contains(it.Id));
            }
        }
    }
}
